<?php
class Comment extends Eloquent {
    
    use SoftDeletingTrait;

    protected $guarded = array('');
    
    protected $touches = array('post');

    protected $dates = ['deleted_at'];

    public static function createComment($data)
    {
    	
    	$comments = new Comment;
        $comments->content = $data['content'];
        $comments->post_id = $data['post_id'];
        $comments->creator_id = Auth::user()->id;        
        $comment = Comment::where('post_id',$data['post_id'])->orderBy('sequence_number', 'DESC')->first();
        
        if(!is_null($comment)){
        	
        	$comments->sequence_number=$comment->sequence_number+1;
        	
        }
        $post = Post::find($data['post_id']);
        DB::transaction(function() use($post, $comments){
        	
        	$comments->save();
        	$post_extra = $post->postExtra()->first();
        	$post_extra->increment('comments_count');

        	$post_extra->touch();  //更新时间戳       	
        	$post_extra->save();
        });     
        return $comments;
    }
    

    
    

    public function post()
    {
        return $this->belongsTo('Post');
    }

    public function commentExtra()
    {
        return $this->hasOne('CommentExtra');
    }

    public function creatorName()
    {
        $user = User::find($this->creator_id);
        $customer = Customer::where('user_id', $this->creator_id)->first();
        if ($customer)
        {
            return $user->realname ? $user->realname : $user->email;
        }
        $company = Company::where('user_id', $this->creator_id)->first();
        if ($company)
        {
            return $company->name ? $company->name : $user->email;
        }
        $lawyer = Lawyer::where('user_id', $this->creator_id)->first();
        if ($lawyer)
        {
            return $user->realname ? $user->realname: $user->email;
        }

        return $user->email;
    }

    public function creatorAvatar()
    {
        $avatar = User::find($this->creator_id)->avatar;
        return UtilsHelper::getImage($avatar, "img/dfimg.png");
    }

    public function creator()
    {
        $customer = Customer::where('user_id', $this->creator_id)->first();
        if ($customer)
        {
            return $customer;
        }

        $company = Company::where('user_id', $this->creator_id)->first();
        if ($company)
        {
            return $company;
        }

        $lawyer = Lawyer::where('user_id', $this->creator_id)->first();
        if ($lawyer)
        {
            return $lawyer;
        }

        return User::find($this->creator_id);
    }
    
    public function owner()
    {
    	return $this->belongsTo('User','creator_id');
    }
    
    /**
     * 登录用户是否已经点赞了这个评论
     * 
     * @param int $id
     */
    public static function isLiked($id){
	if (Auth::user()) {
        	$is_liked = Like::where('item_id', $id)->where('like_type', Like::TYPE_COMMENT)->where('creator_id', Auth::user()->id)->first();
	} else {
		$is_liked = false;
	}
        return (bool)$is_liked;
    }

    /**
     * 这个评论的点赞数
     *
     * @param int $id
     */
    public static function likeCount($id){
        if(empty($id)){
            return false;
        }
        return Like::where('item_id', $id)->where('like_type', Like::TYPE_COMMENT)->count();
    }
/*    
    public static function getComments($posts)
    {	

    	foreach($posts as $rows)
    	{

    		$posts->comments=Comment::where('post_id',$rows->id)->get();
    		


       	}
    	return $posts;

    }
    
 */   
    
}
